db=dict({'zwx':'123','hxy':'456','ss':'111'})
def register():
    flag=True
    while flag:
        for key in db:
            name = input("请输入用户名：")
            if name == key:
                print("用户名已存在，请重新填写用户名")
            else:
                print("用户名注册成功！")
                break
        pwd = input("请输入密码：")
        db[name] = pwd
        pwd2 = input("请确认密码：")
        if pwd2 != pwd:
            print("密码输入有误，请重新输入！")
        else:
            print("注册成功！")
            flag=False
#print(db)
def login():
    count = 0
    flag=True
    while flag :
        for key in db:
            username = input("请输入用户名: ")
            pwd = input("请输入密码: ")
            if username == "" or pwd == "":
                print("您输入的是空值，请重新输入")
                count=count + 1
            elif username != key or pwd != db[key]:
                print("输入的用户名或密码有误请重新输入")
                count = count + 1
            else:
                break;
        if count == 3:
            print("三次机会已用完，无法登录！")
            flag = False
        else:
            print("登录成功！")
            flag=False
def bgoods():
    goods_info = [['苹果',32],['酸奶',43],['电脑',4223],['风扇',123]]
    print('商品信息:',goods_info)
    s=len(goods_info)
    salary = int(input('请输入您的工资:'))
    remain = salary
    goods_buy = []
    goods_kinds = {}
    flag=True
    while flag:
        for i in range(s):#为商品添加编号
            print('{goods}对应编号为:{n}'.format(goods=goods_info[i][0], n=i))
        num = int(input('请输入您要购买的商品编号:'))
        remain = remain - int(goods_info[num][1]) #计算余额，goods_info[num][1]是商品价格
        if remain >= 0:
            goods_buy.append(goods_info[num][0]) #将商品添加到购物车，goods_info[num][0]是商品种类
            select1 = input("继续购买?(y/n):")
            if select1 == "n":
                flag = False
            elif select1 == "y":
                flag = True
            else:
                print("输入内容有误")
        else:
            remain = remain + int(goods_info[num][1]) # 若买完商品后所剩余额小于0，则自动放弃该商品
            select2 = input("余额不足，是否退出?(y/n):");
            if select2 == "y":
                flag = False;
            elif select2 == "n":
                break;
            else:
                print("输入内容有误");

    if goods_buy:
        goods_set = set(goods_buy)
        for i in goods_set:
            goods_kinds[i]=goods_buy.count(i)
        print('您购买的商品及数量为:', goods_kinds)
    else:
        print("未购买任何商品")
    print("余额为：",remain)



select0 = input("注册或登录或退出？（R/L/E）:")
if select0 == "R":
    register()
    select01 = input("登录或退出（L/E）:")
    if select01 == "L":
        login()
        select02 = input("是否购买（Y/N）:")
        if select02 == "Y":
            bgoods()
        elif select02 == "N":
            exit(0)
        else:
            print("输入指令有误")
    elif select01 == "E":
        exit(0)
    else:
        print("输入指令有误")
elif select0 == "L":
    login()
    select03 = input("是否购买（Y/N）:")
    if select03 == "Y":
        bgoods()
    elif select03 == "N":
        exit(0)
    else:
        print("输入指令有误")

elif select0 == "E":
    exit(0)
else:
    print("输入指令有误")


